/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hw;

import Config.Config;
import Model.Persona;
import Model.Proyecto;
import Model.Tarea;
import Util.ActiveDirectory;
import Util.Language;
import com.sun.org.apache.bcel.internal.generic.DADD;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.stage.Stage;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author ddavilac
 */
public class HW extends Application {
    
    @Override
    public void start(Stage stage){   
        try {
            System.out.print("Inicio");  
            
            //Carga el lenguaje de la aplicacion, el idioma por default es "es" (español)                                  
            Language.loadLanguage("Config/lang.es.properties");           
                       
            //Cargar las configuraciones del cliente
            if(!Config.isLoaded()){                                        
                Properties pros = new Properties();
                //Mostrar la ventana para ingresar los primeros datos de configuracion.                                                    
                
                Config.saveConfigs(pros);                                
            } 
            
           
            
            /*
            final Group root = new Group();
                           
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    System.out.println("Hello World!");
                    root.getChildren().set(1, new ImageView(new Image("file:d:\\titan.jpg")));                                                                                              
                }
            });                                                      
        
            root.getChildren().add(btn);               
            root.getChildren().add(new ImageView(new Image("file:d:\\dog.gif")));
            
            Scene scene = new Scene(root, 300, 200, Color.TRANSPARENT);
            //primaryStage.initStyle(StageStyle.TRANSPARENT);
            
            Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
            stage.setX(primaryScreenBounds.getMinX());
            stage.setY(primaryScreenBounds.getMinY());                
            stage.getIcons().add(new Image("http://files.softicons.com/download/application-icons/pixelophilia-icons-by-omercetin/png/32/apple-green.png"));
            stage.setTitle("Hello World!");
            stage.setScene(scene);
            stage.show();
            */
                                         
            /*ActiveDirectory ad = new ActiveDirectory(
                                    Config.getProperty("DCLoginName"), 
                                    Config.getProperty("DCLoginPss"), 
                                    Config.getProperty("DCName"), 
                                    Config.getProperty("DCPort"));
                                     */
            ActiveDirectory ad = new ActiveDirectory(
                                    "ddavilac", 
                                    "108SEcretica108!", 
                                    "epmtelco.com.co", 
                                    "389");
            //Busca al usuario logueado en Windows
            ArrayList tempArrayList = ad.searchUser(System.getProperty("user.name"));        
            
            //Carga el usuario en memoria   
            Persona p = null;
            for(int i =0; i < tempArrayList.size(); i++){
                Attributes attrs = ((SearchResult)tempArrayList.get(i)).getAttributes();                               
                p = new Persona(attrs); 
            } 
            
            if(p == null)   
                System.exit(0);
            
            Tarea t = new Tarea();
            t.setEstado(Tarea.EN_PROCESO);
            
            Proyecto pr = new Proyecto();
            pr.setNombre("Proyecto 1");
            pr.setPresupuesto("2000000");
            pr.setLider(p);
            pr.setCreador(p);
            pr.getTareas().add(t);
            
            
            Session session = new AnnotationConfiguration().configure().buildSessionFactory().openSession();                       
            session.beginTransaction();
            session.save(p);
            session.save(t);
            session.save(pr);
            session.getTransaction().commit();
            //session.close();
            
            
            System.out.print("Fin");
            System.exit(0);
        } catch (Exception ex) {
            Logger.getLogger(HW.class.getName()).log(Level.SEVERE, null, ex);            
            System.exit(0);
        }
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {                       
        launch(args);
    }
}
